import java.util.Arrays;
import java.util.Scanner;

public class Bookshelf implements Normaluser,Administrator{
    private Book[] books=new Book[100];
    public int capacity=100;

    public int cur=0;

    public Bookshelf(){
        init();
    }


    private void wide(){
        books= Arrays.copyOf(books,capacity*2);
    }
    public void add(String name,double price,String author){
        if(cur==capacity){


            wide();
            if(books.length==2*capacity){
                System.out.println("扩容成功");
                capacity*=2;
            }

        }
            Book a=new Book();
            a.name=name;
            a.author=author;
            a.isBorrow=false;
            a.price=price;
            books[cur]=a;

            cur++;




    }

    public void add(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要添加的书的书名");
        String name=sc.nextLine();
        System.out.println("请输入你要添加的书的作者");
        String author=sc.nextLine();
        System.out.println("请输入书的价格");
        double price=sc.nextDouble();

        Book a=new Book(name,author,price,false);
        books[cur]=a;


        cur++;



    }



    public void print(){
        for (int i = 0; i <cur ; i++) {
            System.out.println(books[i]);
        }

    }


    private void init(){


        books[0]=new Book("aaaaaa","未知",50,false);
        books[1]=new Book("bbbbbb","未知",50,false);
        books[2]=new Book("cccccc","未知",50,false);
        cur=3;

    }


    public int check(String name){

        for (int i = 0; i <cur; i++) {
            if(books[i].name.equals(name)){
                return i;

            }

        }
        return -1;


    }


    public void pop(String name){
        int pos=check(name);
        if(pos!=-1){
            books[pos]=null;
            for (int i = pos; i <cur-1 ; i++) {

                books[i]=books[i+1];

            }
            cur--;

        }else{
            System.out.println("没有这本书");
        }



    }


    public void amendStatus(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要添加的书的书名");
        String name=sc.nextLine();
        int index=check(name);

        System.out.println("请输入要修改的书的价格,若保持原价则输入-1");
        double price=sc.nextDouble();

        if(index!=-1){
            books[index].price=price;
        }

        System.out.println("书是否被借出，请输入“true”或false");
        String a=sc.nextLine();
        if (a.equals("true")) {
            books[index].isBorrow=true;
        }else if(a.equals("false")){
            books[index].isBorrow = false;
        }else{
            System.out.println("输入错误");
        }




    }


    public void  checkBook(){
        Scanner sc=new Scanner(System.in);

        System.out.println("请输入你想要查找的书");

        String name=sc.nextLine();

        int n=check(name);

        if(n==-1){
            System.out.println("不存在这本书");
        }else {
            System.out.println("以下是你要查找书的信息");
            System.out.println(books[n]);
        }





    }

    @Override
   public void borrow() {
        Scanner sc=new Scanner(System.in);

        System.out.println("请输入你想借出的书名");

        String name=sc.nextLine();

        int n=check(name);

        if(n==-1){
            System.out.println("不存在这本书");
        }else {
            if(books[n].isBorrow){
                System.out.println("这本书已经被借出了");
            }else
            {

            books[n].isBorrow=true;
                System.out.println("借阅成功");
            }
        }


    }

    @Override
   public void returnBook() {
        Scanner sc=new Scanner(System.in);

        System.out.println("请输入你要还书的书名");

        String name=sc.nextLine();

        int n=check(name);

        if(n==-1){
            System.out.println("不存在这本书");
        }else {
            books[n].isBorrow=false;
            System.out.println("还书成功");
        }

    }


}